package com.panshi.shiro;

import com.alibaba.fastjson.JSON;
import com.panshi.api.UserApi;
import com.panshi.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
public class PhoneRealm extends AuthorizingRealm {

    @Autowired
    private UserApi userApi;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

        return null;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        PhoneEmailToken token = null;
        String principal =(String) token.getPrincipal();

        Object userMemberStr = userApi.userLogin(principal).getData().get("data");
        UserVO userVO = JSON.parseObject(userMemberStr.toString(), UserVO.class);
        log.info("认证=============================={}====================================>{}",principal,userVO);
        if(userVO != null){
            return new SimpleAuthenticationInfo(principal,userVO.getPassword(),this.getName());
        }

        return null;
    }
}
